<?php


namespace app\admin\controller\mall;
use app\admin\model\MallGoods;
use app\admin\model\MallOrder;
use app\admin\model\MallTradeRecordLog;
use app\admin\model\MallUser;
use app\admin\model\MallWithdrawRecord;
use app\admin\model\SystemAdmin;
use app\api\help\SendMsg;
use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;
use think\facade\Db;
/**
 * @ControllerAnnotation(title="提现已打款")
 */

class AuditStatus2 extends AdminController
{
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new MallWithdrawRecord();
    }
    
    /**
     * @NodeAnotation(title="已打款")
     */
    public function payed()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            [$page, $limit, $where] = $this->buildTableParames();
            $where[]   = ['ea_mall_withdraw_record.status', 'in', [2]];
            $count     = $this->model
                ->withJoin(['user'], 'left')
                ->where($where)
                ->count();
            $list      = $this->model
                ->withJoin(['user'], 'left')
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select()
                ->toArray();
            $adminIds1 = array_column($list, 'approve_admin_id');
            $adminIds2 = array_column($list, 'pay_admin_id');
            $adminIds  = array_merge($adminIds1, $adminIds2);
            $admins    = SystemAdmin::where([['id', 'in', $adminIds]])->select();
            $admins    = array_column($admins->toArray(), 'username', 'id');
            foreach ($list as $key => &$value) {
                $value[ 'approve_time' ] = $value[ 'approve_time' ] ? date('Y-m-d H:i:s', $value[ 'approve_time' ]) : '';
                $value[ 'pay_time' ]     = $value[ 'pay_time' ] ? date('Y-m-d H:i:s', $value[ 'pay_time' ]) : '';
                $value[ 'approve_user' ] = $admins[ $value[ 'approve_admin_id' ] ] ?? '';
                $value[ 'pay_user' ]     = $admins[ $value[ 'pay_admin_id' ] ] ?? '';
            }
            $total  = $this->model
                ->withJoin(['user'], 'left')
                ->where($where)
                ->value('SUM(amount)');
            $newArr = ['type' => '', 'user' => ['name' => '总计', 'mobile' => ''], 'amount' => $total,'status'=>''];
            array_unshift($list, $newArr);
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }
}